/**
 * 文件名：ExcelListener
 * 版权：Copyright 2017-2022 CMCC All Rights Reserved.
 * 描述：
 */

package com.hand.project.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hand.project.config.SpringContextUtil;
import com.hand.project.domain.Staff;
import com.hand.project.service.impl.StaffServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * The type Excel listener.
 * 解析监听器，
 * 每解析一行会回调invoke()方法。
 * 整个excel解析结束会执行doAfterAllAnalysed()方法
 *
 *
 * @author shuai.wang02 @hand-china.com
 * @version 1.0
 * @date 2018 /11/12 15:09
 */
@Component
public class ExcelListener extends AnalysisEventListener {



    private StaffServiceImpl staffServiceImpl;

    /**
     * 获取staffServiceImpl bean
     */
    public ExcelListener() {
           this.staffServiceImpl=(StaffServiceImpl) SpringContextUtil.getBean("staffServiceImpl");
    }

    @Override
    public void invoke(Object object, AnalysisContext context) {
        doSomething(object);
    }

    private void doSomething(Object object) {
        Staff staff =  (Staff)object;
        staffServiceImpl.saveStaff(staff);
    }


    /**
     * 整个excel解析结束会执行doAfterAllAnalysed()方法
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // datas.clear();//解析结束销毁不用的资源
    }

}
